package com.example.yls.cnbetaapp.amqp;
import android.util.Log;
import android.widget.LinearLayout;

import com.example.yls.cnbetaapp.util.ChannelUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeoutException;

public class RPCClient {
    /**
     *
     * @param queueName
     * @param exchangeName
     * @param replayToName
     * @param message
     */
    public String getServerContent(String routingKey,String queueName,String exchangeName,String replayToName,String message){
        final BlockingQueue blockingQueue = new ArrayBlockingQueue(20);
        String receive = null;
        try {
            final Channel channel = ChannelUtils.getChannel("Cnbeta手机端");
            channel.queueDeclare(queueName, true, false, false, (Map)new HashMap<>());
            channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, (Map)new HashMap<>());
            channel.queueBind(queueName, exchangeName, routingKey, (Map)new HashMap<>());
            String replyTo = replayToName;
            channel.queueDeclare(replyTo, true, false, false, (Map)new HashMap<>());
            final String correlationId = UUID.randomUUID().toString();
            AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder().deliveryMode(2).contentType("UTF-8").correlationId(correlationId).replyTo(replyTo).build();
            channel.basicPublish(exchangeName, routingKey, true, basicProperties, message.getBytes());
            channel.basicConsume(replayToName, true, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    Log.e("resultTag","----------RPC调用结果----------");
//                    Log.e("comsumerTag:",consumerTag);
//                    Log.e("消息内容为",new String(body));
                       String s = properties.getCorrelationId();
//                      if(properties.getCorrelationId().equals(correlationId)){
                          try {
                              blockingQueue.put(new String(body));
                              channel.close();
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          } catch (TimeoutException e) {
                              e.printStackTrace();
                          }
//                      }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
             receive = (String) blockingQueue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return receive;
    }

}
